package com.ruoyi.wmsorder.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.wmsorder.domain.vo.WmsPurchaseOrderDetailVO;
import com.ruoyi.wmsorder.domain.vo.WmsPurchaseorderPoPVO;
import com.ruoyi.wmsorder.domain.vo.WmsPurchaseorderVO;
import com.ruoyi.wmsorder.domain.vo.WmsPurchaseorderVOS;
import com.ruoyi.wmsorder.quartz.config.OderNos;
import com.ruoyi.wmsorder.quartz.config.SerialNumber;
import com.sun.jna.platform.win32.OaIdl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.wmsorder.domain.WmsPurchaseorderDetail;
import com.ruoyi.wmsorder.mapper.WmsPurchaseorderMapper;
import com.ruoyi.wmsorder.domain.WmsPurchaseorder;
import com.ruoyi.wmsorder.service.IWmsPurchaseorderService;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 采购订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-12
 */
@Service
@Slf4j
public class WmsPurchaseorderServiceImpl implements IWmsPurchaseorderService 
{
    @Override
    public List<WmsPurchaseorderVOS> selectOrderList() {
        return null;
    }

    @Autowired
    private WmsPurchaseorderMapper wmsPurchaseorderMapper;
//    采购订单编号流水号初始值
    private  static   Integer serialNUm=1000;
    /**
     * 查询采购订单
     * 
     * @param id 采购订单主键
     * @return 采购订单
     */
    @Override
    public List<WmsPurchaseOrderDetailVO> selectWmsPurchaseorderById(Long id)
    {

        System.out.println("子表内容："+wmsPurchaseorderMapper.selectWmsPurchaseorderById(id));
        return wmsPurchaseorderMapper.selectWmsPurchaseorderById(id);
    }

    /**
     * 查询采购订单列表
     * 
     * @param wmsPurchaseorder 采购订单
     * @return 采购订单
     */
    @Override
    public List<WmsPurchaseorderVO> selectWmsPurchaseorderList(WmsPurchaseorder wmsPurchaseorder)
    {


        return wmsPurchaseorderMapper.selectWmsPurchaseorderList(wmsPurchaseorder);
    }

    /**
     * 查询采购订单弹窗列表
     * @param wmsPurchaseorder
     * @return
     */

    @Override
    public List<WmsPurchaseorderPoPVO> selectWmsPurchaseorderPopList(WmsPurchaseorder wmsPurchaseorder) {

        log.error("查询采购订单弹窗列表"+wmsPurchaseorderMapper.selectWmsPurchaseorderPoPList(wmsPurchaseorder));
        return wmsPurchaseorderMapper.selectWmsPurchaseorderPoPList(wmsPurchaseorder);
    }

    /**
     * 新增采购订单
     * 
     * @param wmsPurchaseorder 采购订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWmsPurchaseorder(WmsPurchaseorder wmsPurchaseorder)
    {


        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

        Date date = new Date();
        String orderNo = OderNos.generateOrderNo(SerialNumber.PURCHASE_PREFIX, "yyyyMMdd");
        wmsPurchaseorder.setOrderNo(orderNo);
        wmsPurchaseorder.setCreateTime(DateUtils.getNowDate());
        wmsPurchaseorder.setUpdateTime(DateUtils.getNowDate());
        int rows = wmsPurchaseorderMapper.insertWmsPurchaseorder(wmsPurchaseorder);
        insertWmsPurchaseorderDetail(wmsPurchaseorder);
        return rows;
    }

    /**
     * 修改采购订单
     * 
     * @param wmsPurchaseorder 采购订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWmsPurchaseorder(WmsPurchaseorder wmsPurchaseorder)
    {
        wmsPurchaseorder.setUpdateTime(DateUtils.getNowDate());
        wmsPurchaseorderMapper.deleteWmsPurchaseorderDetailByOrderId(wmsPurchaseorder.getId());
        insertWmsPurchaseorderDetail(wmsPurchaseorder);
        return wmsPurchaseorderMapper.updateWmsPurchaseorder(wmsPurchaseorder);
    }

    /**
     * 批量删除采购订单
     * 
     * @param ids 需要删除的采购订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsPurchaseorderByIds(Long[] ids)
    {
        wmsPurchaseorderMapper.deleteWmsPurchaseorderDetailByOrderIds(ids);
        return wmsPurchaseorderMapper.deleteWmsPurchaseorderByIds(ids);
    }

    /**
     * 删除采购订单信息
     * 
     * @param id 采购订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsPurchaseorderById(Long id)
    {
        wmsPurchaseorderMapper.deleteWmsPurchaseorderDetailByOrderId(id);
        return wmsPurchaseorderMapper.deleteWmsPurchaseorderById(id);
    }

    /**
     * 新增采购订单详情信息
     * 
     * @param wmsPurchaseorder 采购订单对象
     */
    public void insertWmsPurchaseorderDetail(WmsPurchaseorder wmsPurchaseorder)
    {
        List<WmsPurchaseorderDetail> wmsPurchaseorderDetailList = wmsPurchaseorder.getWmsPurchaseorderDetailList();
        //主表id
        Long id = wmsPurchaseorder.getId();


        if (StringUtils.isNotNull(wmsPurchaseorderDetailList))
        {
            List<WmsPurchaseorderDetail> list = new ArrayList<WmsPurchaseorderDetail>();
            for (WmsPurchaseorderDetail wmsPurchaseorderDetail : wmsPurchaseorderDetailList)
            {
                wmsPurchaseorderDetail.setOrderId(id);
                wmsPurchaseorderDetail.setCreateTime(DateUtils.getNowDate());
                wmsPurchaseorderDetail.setUpdateTime(DateUtils.getNowDate());
                wmsPurchaseorderDetail.setCreateById(getUserId());
                wmsPurchaseorderDetail.setUpdateById(getUserId());
                list.add(wmsPurchaseorderDetail);
            }
            if (list.size() > 0)
            {
                wmsPurchaseorderMapper.batchWmsPurchaseorderDetail(list);
            }
        }
    }


}
